<?php
/**
 * Created by PhpStorm.
 * User: MrHung
 * Date: 5/12/14
 * Time: 11:27 AM
 */

namespace Admin\Model;
use Admin\Model\AdminUser;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Exception;

class AdminPageTable {


    protected $tableGateway;

    /**
     * @param TableGateway $tableGateway
     */
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }

    /**
     * @param null $table
     */
    public function optimizeDB($table= null){
        $query = " ANALYZE TABLE `".$table.'`' ;
        $driver = $this->tableGateway->getAdapter()->getDriver();
        $connection = $driver->getConnection();
        $result = $connection->execute($query);
        $query = " OPTIMIZE TABLE `".$table.'`' ;
        $driver = $this->tableGateway->getAdapter()->getDriver();
        $connection = $driver->getConnection();
        $result = $connection->execute($query);
    }

    public function fetchAll($where = null,$order = null)
    {
        $select = new Select('page');
        if($where != null) $select->where($where);
       if($order != null) $select->order($order);
        // create a new result set based on the Album entity
        $resultSetPrototype = new ResultSet();
        $resultSetPrototype->setArrayObjectPrototype(new AdminPage());
        // create a new pagination adapter object

        $paginatorAdapter = new DbSelect(
        // our configured select object
            $select,
            // the adapter to run it against
            $this->tableGateway->getAdapter(),
            // the result set to hydrate
            $resultSetPrototype
        );
        $paginator = new Paginator($paginatorAdapter);
        return $paginator;
    }

    /**
     * @param AdminPage $page
     * @return int|null
     */
    public function savePage(AdminPage $page)
    {

        $data = array(
            'title' => $page->title,
            'content' => $page->content,
            'page_date' => $page->page_date,
            'author' => $page->author,
            'status' => $page->status,
            'page_parent' => $page->page_parent,
            'page_order' => $page->page_order,
            'page_type' => $page->page_type,
            'slug' => $page->slug,

        );

        $id = (int)$page->id;
        if ($id == 0) {
            $this->tableGateway->insert($data);
            return $this->tableGateway->getLastInsertValue();
        } else {
            if ($this->getPage($id)) {
                $this->tableGateway->update($data, array('id' => $id));
                return $id;
            } else {
                return null;
            }
        }
    }

    /**
     * @param $id
     * @return array|\ArrayObject|null
     * @throws \Exception
     */

    public function getPage($id)
    {
        $id = (int)$id;
        $rowset = $this->tableGateway->select(array('id' => $id));
        $row = $rowset->current();

        if (!$row) {
            throw new \Exception("Could not find row $id");
        }
        return $row;
    }

    /**
     * @param $id
     */
    public function deletePage($id)
    {
        $this->tableGateway->delete(array('id' => $id));
    }

    /**
     * @param $slug
     * @return array|\ArrayObject|null
     * @throws \Exception
     */
    public function getPageByslug($slug)
    {

        $rowset = $this->tableGateway->select(array('slug' => $slug));
        $row = $rowset->current();

        if (!$row) {
            //throw new \Exception("Could not find row $slug");
            return null;
        }
        return $row;
    }


} 